package tawai;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.game.Sprite;

/**
 * Class models tank shells
 *
 * @author Alex
 */
public class Shell implements IterativelyActive, Drawable {

    /**
     * Shell's sprite
     */
    private Sprite sprite;

    /**
     * Shell's direction in degrees relative to x-axis counterclockwise
     */
    private int dir;

    /**
     * Shell moving step
     * It flies this amount of pixels on each global loop iteration
     */
    private int step = 8;

    /**
     * Constructor<br>
     * Sprite direction adjustment and other initialization is done here
     * 
     * @param initDir Shell's moving direction in degrees relative to x-axis counterclockwise
     * @param initX Shell's initial <code>x</code>-coordinate
     * @param initY Shell's initial <code>y</code>-coordinate
     */
    public Shell(int initDir, int initX, int initY) {

        dir = initDir;
        try {
            sprite = new Sprite(TawaiMIDlet.getMain().getGameDesign().getShellSprite());
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        // Adjusting sprite direction
        sprite.setPosition(initX, initY);
        switch (dir) {
            case 0:
                sprite.setTransform(Sprite.TRANS_ROT90);
                break;
            case 90:
                sprite.setTransform(Sprite.TRANS_NONE);
                break;
            case 180:
                sprite.setTransform(Sprite.TRANS_ROT270);
                break;
            case 270:
                sprite.setTransform(Sprite.TRANS_ROT180);
                break;
        }
    }

    /**
     * @see Drawable
     */
    public Sprite getSprite() {

        return sprite;
    }

    /**
     * Shell's iterative action lies in moving along gamefield
     * while checking collisions with tanks and obstacles
     *
     * @see IterativelyActive
     */
    public void performIterativeAction() {

        // Check whether the shell collides with any obstacles
        try {
            if (sprite.collidesWith(TawaiMIDlet.getMain().getGameDesign().getObstaclesLayer(), false)) {
                explode(); // Explodes if collision occurs
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        // This is done to check collisions with tanks
        Vector collidees = TawaiMIDlet.getMain().checkCollisions(sprite);
        if (collidees.size() != 0)
            for(int index = 0; index < collidees.size(); index++)
                if (collidees.elementAt(index) instanceof Tank) {
                    ((Tank) collidees.elementAt(index)).die();
                    explode();
                }

        // Shell moves forward
        switch(dir) {
            case 0:
                sprite.setPosition(sprite.getX() + step, sprite.getY());
                break;
            case 90:
                sprite.setPosition(sprite.getX(), sprite.getY() - step);
                break;
            case 180:
                sprite.setPosition(sprite.getX() - step, sprite.getY());
                break;
            case 270:
                sprite.setPosition(sprite.getX(), sprite.getY() + step);
                break;
        }
    }

    /**
     * Called when shell should explodes<br>
     * 
     */
    private void explode() {

        // Creating new explosion object
        Sprite explosion = null;
        try {
            explosion = new Sprite(TawaiMIDlet.getMain().getGameDesign().getShellExplosionSprite());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        explosion.setPosition(sprite.getX(), sprite.getY());

        // Adding new Explosion onto the scene
        TawaiMIDlet.getMain().addSceneObject(new Explosion(explosion));
        // Removing shell from scene
        TawaiMIDlet.getMain().removeSceneObject(this);
    }
    
}
